package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BtOperation;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味信息
     * @param dishDto
     */
    @Transactional      // 操纵多表，加入事务控制
    public void saveWithFlavor(DishDto dishDto) {

        // 保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        // 获取菜品id
        Long dishId = dishDto.getId();

        // 将 id 赋值给 flavors 集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        // 保存菜品口味数据到菜品口味表dish_flavors
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据 id 查询对应的菜品信息和口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id) {

        // 1. 查询菜品基本信息
        Dish dish = this.getById(id);

        // 对象拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        // 2. 查询菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品信息和菜品口味信息
     * @param dishDto
     */
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        // 根据 id 更新菜品信息
        this.updateById(dishDto);

        // 获取菜品id
        Long dishId = dishDto.getId();

        // 清理原先菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(queryWrapper);

        // 获取新菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 对口味信息附加 菜品id 信息
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        // 对菜品口味信息进行保存
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 批量删除菜品，同时删除菜品对应的口味信息
     * @param btOperation
     */
    @Transactional  // 添加事务处理
    public boolean deleteWithFlavor(BtOperation btOperation) {

        // 获取菜品的id
        List<Long> ids = btOperation.getIds();

        for (Long id : ids) {
            Dish dish = this.getById(id);
            if (dish.getStatus() != 0) return false;
        }

        // 根据id删除对应的菜品
        this.removeByIds(ids);

        // 删除 菜品id 在口味表中对应的数据
        for (Long id:
             ids) {
            // 构造条件
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
        }

        return true;
    }

}
